module windump.util;

import std.string;
import std.stream;
import std.path;
import std.date;
import std.utf;


version(WD_Unittest)
    import std.stdio;

import windump.w32api;
import windump.utf;


void messageBox(string text)
{
    MessageBoxA(null, toStringz(text), null,
            MB_OK | MB_ICONEXCLAMATION);
}


string getUserProfileDirectory()
{
    const uint MaxPath = 2048;
    wchar[MaxPath] userName;
    DWORD userNameLen = MaxPath;
    
    GetUserNameW(userName.ptr, &userNameLen);

    wchar[MaxPath] pfdir;
    DWORD pfdirLen = MaxPath;
    
    GetProfilesDirectoryW(pfdir.ptr, &pfdirLen);

    return toUTF8(pfdir[0..pfdirLen - 1] ~ "\\" ~ userName[0..userNameLen - 1]);
}

string getModulePath()
{
    wchar[MAX_PATH] wfilename;
    uint fnlen = GetModuleFileNameW(null, wfilename.ptr, MAX_PATH);
    string filename = toUTF8(wfilename[0 .. fnlen - 1]);
    return getDirName(filename);
}



class IniFile
{
    private string[string] m_content;

    public  this(string path)
    {
        auto rawFile = new File;
        rawFile.open(path);
        auto file = new BufferedStream(rawFile);
        
		foreach(char[] line; file)
        {
            int pos = find(line, '=');
            if(pos <= 0)continue;
            version(D_Version2) m_content[line[0..pos].idup] = line[pos + 1 .. $].idup;
            else m_content[line[0..pos].dup] = line[pos + 1 .. $].dup;
        }
    }

    public string opIndex(string key) {
        return m_content[key];
    }

}
